﻿using System;
using System.Collections.Generic;
using System.Text;
using SchEdu.Framework.Validators;
using SchEdu.Framework.Logging;
using SchEdu.Framework.DataModel;
using SchEdu.Framework.Exceptions;

namespace SchEdu.Framework.Constraint
{
    public abstract class Constraint : IConstraint
    {
        SchEduData schEduData;

        public SchEduData SchEduData
        {
            get { return schEduData; }
            set { schEduData = value; }
        }

        SchEduDataSet.AllocationRow allocation;

        public SchEduDataSet.AllocationRow Allocation
        {
            get { return allocation; }
            set { allocation = value; }
        }

        protected SchEduDataSet.ConstraintRow constraint;

        protected ValidatorList validators = new ValidatorList();

        private Logger logger = new Logger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString());

        protected Logger Logger
        {
            get { return logger; }
            set { logger = value; }
        }

        public Constraint()
        {
        }

        public Constraint(SchEduData schEduData, SchEduDataSet.AllocationRow allocation)
        {
            this.schEduData = schEduData;
            this.Allocation = allocation;
        }

        #region IConstraint Members

        public virtual void Run()
        {
            Initialize();

            Validate();

            Logic();

        }

        public abstract void Logic();

        private void Initialize()
        {
            InitializeValidators();
        }


        protected virtual void InitializeValidators()
        {
            validators = new ValidatorList();
        }

        protected virtual void Validate()
        {
            foreach (IValidator validator in validators)
            {
                validator.Validate();
            }
        }

        public abstract AboutInfo AboutInfo
        {
            get;
        }

        #endregion

        protected void GenerateConstraintRow()
        {
            constraint = schEduData.Constraint.NewConstraintRow();
            constraint.AllocationID = Allocation.ID;
            constraint.Title = AboutInfo.Title;
            constraint.Version = AboutInfo.Version;
            constraint.Company = AboutInfo.Company;
            constraint.Copyright = AboutInfo.Copyright;
            constraint.Description = AboutInfo.Description;
            constraint.Product = AboutInfo.Product;
            schEduData.Constraint.AddConstraintRow(constraint);
        }

    }
}
